package com.example.customize.service.base;

import ch.qos.logback.classic.db.names.TableName;
import com.example.customize.dao.base.CrudDao;
import com.example.customize.service.base.CrudService;
import com.example.customize.util.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

public class CrudServiceImpl<T,VO,QO> implements CrudService<T,VO,QO> {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	protected CrudDao<T,VO,QO> dao;
	
	@Override
	public T query4id(String id) {
		return dao.query4id(id);
	}
	
	@Override
	public VO query4id2VO(String id) {
		if (StringUtils.isEmpty(id)){
			return null;
		}
		T t = dao.query4id(id);
		Class voClass = Reflections.getClassGenricType(this.getClass(), 1);
		VO vo = null;
		try {
			vo = (VO) voClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			logger.error("根据ID查询返回VO对象转换异常",e);
		}
		BeanUtils.copyProperties(t,vo);
		return vo;
	}

	@Override
	public boolean create(T entity) {
		return dao.create(entity);
	}
	
	public boolean create(T entity,TableName tableName) {
		return dao.create(entity);
	}

	@Override
	public boolean modify(T entity) {
		return dao.modify(entity);
	}

	@Override
	public boolean remove(String id) {
		return dao.remove(id);
	}

	@Override
	public List<VO> findList(QO query) {
		return dao.findList(query);
	}

	@Override
	public long findCount(QO query) {
		return dao.findCount(query);
	}

}
